*=============================
*This do file clean and process the lab data
*four related folders:
*data: store the data
*log: store the lob file
*graph: export the graphs
*output: export the tables
*=============================





*=============================
*cleanup raw data
*there are 14 sessions. 
*for sessions 9-14, the participate id is assigned in the codes
* (this was done manually in the input excel file for sessions 1-8) 
*=============================


foreach i of numlist 1/14{
import excel using data/session`i'.xlsx, firstrow clear
*delete the experimenter who tested the program in the first session
drop if participantid_in_session==12 & `i'==1

*define subject id in treatment 9
replace participantid_in_session =	11	if `i'==9 & sessioncode== "24045f72"
replace participantid_in_session =	12	if `i'==9 & sessioncode== "q07gd3eg"
replace participantid_in_session =	13	if `i'==9 & sessioncode== "v3f2v4oh"
replace participantid_in_session =	14	if `i'==9 & sessioncode== "0gylmwgr"
replace participantid_in_session =	15	if `i'==9 & sessioncode== "kiuf0uaq"
replace participantid_in_session =	16	if `i'==9 & sessioncode== "qxn5nn1n"
replace participantid_in_session =	17	if `i'==9 & sessioncode== "20p6kznh"
replace participantid_in_session =	18	if `i'==9 & sessioncode== "pk5dcn05"
*for treatment 10
replace participantid_in_session=	1	if `i'==10 & sessioncode==	"7ub64qhy"	
replace participantid_in_session=	2	if `i'==10 & sessioncode==	"igp91vy2"	
replace participantid_in_session=	3	if `i'==10 & sessioncode==	"91abc6au"	
replace participantid_in_session=	4	if `i'==10 & sessioncode==	"s6gzcld1"	
replace participantid_in_session=	5	if `i'==10 & sessioncode==	"emrhm2sv"	
replace participantid_in_session=	6	if `i'==10 & sessioncode==	"keomsgcw"	
replace participantid_in_session=	7	if `i'==10 & sessioncode==	"zfjypco0"	
replace participantid_in_session=	8	if `i'==10 & sessioncode==	"e6vonb6e"	
replace participantid_in_session=	9	if `i'==10 & sessioncode==	"0ezz7n7m"	
replace participantid_in_session=	10	if `i'==10 & sessioncode==	"p6yvcjri"	
replace participantid_in_session=	11	if `i'==10 & sessioncode==	"a7o3lgcu"	
replace participantid_in_session=	12	if `i'==10 & sessioncode==	"an6ble34"	
replace participantid_in_session=	13	if `i'==10 & sessioncode==	"8ah38fzi"	
replace participantid_in_session=	14	if `i'==10 &  sessioncode==	"gso3u42l"	
replace participantid_in_session=	15	if `i'==10 & sessioncode==	"ovjq52ow"	
*for treatment 11
replace participantid_in_session=	1	if `i'==11 & sessioncode==	"1d3lfmdk"
replace participantid_in_session=	2	if `i'==11 & sessioncode==	"3zuklrtf"
replace participantid_in_session=	3	if `i'==11 & sessioncode==	"6bk9vb19"
replace participantid_in_session=	4	if `i'==11 & sessioncode==	"6pd7tbsg"
replace participantid_in_session=	5	if `i'==11 & sessioncode==	"at3cqqt8"
replace participantid_in_session=	6	if `i'==11 & sessioncode==	"eeiv2tcs"
replace participantid_in_session=	7	if `i'==11 & sessioncode==	"felsoqua"
replace participantid_in_session=	8	if `i'==11 & sessioncode==	"ijdhk2p3"
replace participantid_in_session=	9	if `i'==11 & sessioncode==	"pfar3wdl"
replace participantid_in_session=	10	if `i'==11 & sessioncode==	"t2gfi933"
replace participantid_in_session=	11	if `i'==11 & sessioncode==	"tbgnqy2n"
replace participantid_in_session=	12	if `i'==11 & sessioncode==	"xwzz9r6u"
replace participantid_in_session=	13	if `i'==11 & sessioncode==	"z3acax16"
*for treatment 12
replace participantid_in_session=	1	if `i'==12 & sessioncode==	"363ktvjd"
replace participantid_in_session=	2	if `i'==12 & sessioncode==	"3iyiv7u5"
replace participantid_in_session=	3	if `i'==12 & sessioncode==	"3p15rjwx"
replace participantid_in_session=	4	if `i'==12 & sessioncode==	"44hx87af"
replace participantid_in_session=	5	if `i'==12 & sessioncode==	"5bi430cf"
replace participantid_in_session=	6	if `i'==12 & sessioncode==	"czssr8qp"
replace participantid_in_session=	7	if `i'==12 & sessioncode==	"elqkhjgt"
replace participantid_in_session=	8	if `i'==12 & sessioncode==	"jhi779b2"
replace participantid_in_session=	9	if `i'==12 & sessioncode==	"ju6k8xxg"
replace participantid_in_session=	10	if `i'==12 & sessioncode==	"k83yj7sh"
replace participantid_in_session=	11	if `i'==12 & sessioncode==	"pj3ko5b4"
replace participantid_in_session=	12	if `i'==12 & sessioncode==	"qisfti27"
replace participantid_in_session=	13	if `i'==12 & sessioncode==	"uyb4rdf0"
replace participantid_in_session=	14	if `i'==12 & sessioncode==	"vnd1ek5g"
replace participantid_in_session=	15	if `i'==12 & sessioncode==	"wzwtii19"
replace participantid_in_session=	16	if `i'==12 & sessioncode==	"xpw41oui"
replace participantid_in_session=	17	if `i'==12 & sessioncode==	"yysh2y8p"
replace participantid_in_session=	18	if `i'==12 & sessioncode==	"zabjapvk"
*for treatment 13
replace participantid_in_session=	1	if `i'==13 & sessioncode==	"3qdhyod5"
replace participantid_in_session=	2	if `i'==13 & sessioncode==	"6gfgcmrh"
replace participantid_in_session=	3	if `i'==13 & sessioncode==	"6u7p6smt"
replace participantid_in_session=	4	if `i'==13 & sessioncode==	"9pa4i89w"
replace participantid_in_session=	5	if `i'==13 & sessioncode==	"d9d1f539"
replace participantid_in_session=	6	if `i'==13 & sessioncode==	"hrxuuptm"
replace participantid_in_session=	7	if `i'==13 & sessioncode==	"slykrgx9"
replace participantid_in_session=	8	if `i'==13 & sessioncode==	"tgmg4429"
replace participantid_in_session=	9	if `i'==13 & sessioncode==	"v8wu9xwl"
replace participantid_in_session=	10	if `i'==13 & sessioncode==	"xo2wol6k"
replace participantid_in_session=	11	if `i'==13 & sessioncode==	"fpkthl66"
replace participantid_in_session=	12	if `i'==13 & sessioncode==	"0jcvw110"
replace participantid_in_session=	13	if `i'==13 & sessioncode==	"t0bufgpp"
replace participantid_in_session=	14	if `i'==13 & sessioncode==	"8madq3d3"
replace participantid_in_session=	15	if `i'==13 & sessioncode==	"pd6sdq4n"
replace participantid_in_session=	16	if `i'==13 & sessioncode==	"90klohv6"
replace participantid_in_session=	17	if `i'==13 & sessioncode==	"t8rlqj1t"
*for treatment 14
replace participantid_in_session=	1	if `i'==14 & sessioncode==	"0erz51jj"
replace participantid_in_session=	2	if `i'==14 & sessioncode==	"2s5twbv1"
replace participantid_in_session=	3	if `i'==14 & sessioncode==	"5boum1ji"
replace participantid_in_session=	4	if `i'==14 & sessioncode==	"cdgp2xi3"
replace participantid_in_session=	5	if `i'==14 & sessioncode==	"fhzwqjw4"
replace participantid_in_session=	6	if `i'==14 & sessioncode==	"fkpmm16u"
replace participantid_in_session=	7	if `i'==14 & sessioncode==	"of8rn85q"
replace participantid_in_session=	8	if `i'==14 & sessioncode==	"ou5pzwqy"
replace participantid_in_session=	9	if `i'==14 & sessioncode==	"rluklpi8"
replace participantid_in_session=	10	if `i'==14 & sessioncode==	"tgc9wius"
replace participantid_in_session=	11	if `i'==14 & sessioncode==	"vn1bkbme"
replace participantid_in_session=	12	if `i'==14 & sessioncode==	"zpy499wh"

tab participantid_in_session
*subsession.valid_round=1 for valid decision variables
*=0 placeholders
*=2 after experiment surveys
replace subsessionvalid_round=2 if subsessionround_number==110
*remove comments
drop playercomments
drop if subsessionvalid_round==0
*sort by id and round
sort participantid_in_session subsessionround_number
gen session=`i'
drop if subsessionround_number==.
save data/session`i', replace
}


*=============================
*process the data
*=============================


cap log close
log using log\labanalysis, replace
*import experimental data with model predictions
use data/session1, clear
set scheme s2mono
foreach i of numlist 2/14{
append using data/session`i', force
}
*merge with model predictions
append using data/model, force


*assigning treatments
*sessiontype 
*=1, tda, =2 no-tda old tax
* =3 no tda new tax 
* =4, tda no education
* =5 no tda with CA first
*=6, no tda with CA consumption first
gen sessiontype=1 if (session==1) | (session==5)
replace sessiontype=2 if  (session==2) | (session==4) | (session==6)
replace sessiontype=3 if  (session==3) | (session==7) | (session==8)
replace sessiontype=4 if session==9 | session==10
replace sessiontype=5 if session==11 | session==12
replace sessiontype=6 if session==13 | session==14
*assign 0-1 indicator for true/false value
replace playerMC_answer_3="True" if playerMC_answer_3=="1"
replace playerMC_answer_3="False" if playerMC_answer_3=="0"

tab sessiontype if subsessionround_number==110

*generate treatment dummies
gen t1=(sessiontype==1)
gen t2=(sessiontype==2)
gen t3=(sessiontype==3)
gen t4=(sessiontype==4)
gen t5=(sessiontype==5)
gen t6=(sessiontype==6)

*treatment effects 
global treatmentlist="t2 t3 t4 t5 t6"
*treatment effects for contribution rate only
global treatmentlist_ca="t4 t5 t6"

*dummy for whether have a commitment account
gen ca_yes=0
replace ca_yes=1 if sessiontype==1 | sessiontype==4 | sessiontype==5 | sessiontype==6

*model predictions
replace sessiontype=11 if (participant_current_app_name=="tda_model")
replace sessiontype=12 if (participant_current_app_name=="oldtax_model")
replace sessiontype=13 if (participant_current_app_name=="newtax_model")

replace participantid_in_session=1 if sessiontype==11
replace participantid_in_session=1 if sessiontype==12
replace participantid_in_session=1 if sessiontype==13

*assign theory predictions to treatments and models
*tda
gen treatment=1 
*non-tda old tax
replace treatment=2 if (sessiontype==2 | sessiontype==5 | sessiontype==6 | sessiontype==12)
*non-tda new tax
replace treatment=3 if (sessiontype==3 | sessiontype==13)

*id number
gen id=sessiontype*10000+session*100+participantid_in_session
*assign sequence to model predictions
replace subsessionsequence=1 if sessiontype>=11
gen id_sequence=id*10+subsessionsequence



 
*generate survey response summary
* number of corrected crt questions
gen temp=((playeranswer_1==4)+(playeranswer_2==29)+(playeranswer_3==20)+(playeranswer_4=="lost money in the stock market")) if subsessionround_number==110
bysort id: egen crt=mean(temp)
drop temp

* number of correct financial literacy questions
gen temp=((playerMC_answer_1=="More than $102")+(playerMC_answer_2=="Less than today")+(playerMC_answer_3=="False")) if subsessionround_number==110
bysort id: egen financialliteracy=mean(temp)
drop temp

*number of A choices
*risk averse indicator
gen temp=playerrisk_answer_01+playerrisk_answer_02+playerrisk_answer_03+playerrisk_answer_04+playerrisk_answer_05+playerrisk_answer_06/*
*/+playerrisk_answer_07+playerrisk_answer_08+playerrisk_answer_09+playerrisk_answer_10-10 
replace temp=10-temp 
replace temp=. if subsessionround_number~=110
bysort id: egen riskaverse=mean(temp)
drop temp

* not monotonic indicator
gen temp=0 
replace temp=1 if playerrisk_answer_09>playerrisk_answer_10
foreach i of numlist 8(-1)1 {
replace temp=1 if playerrisk_answer_0`i'>playerrisk_answer_0`=`i'+1'
}
replace temp=. if subsessionround_number~=110
bysort id: egen riskaverse_ab=mean(temp)
drop temp

*demographic features
gen temp=(playergender=="Female")
replace temp=. if subsessionround_number~=110
bysort id: egen female=mean(temp)
drop temp

gen temp=1 /*white*/
replace temp=2 if (playerrace=="African American or Black")
replace temp=3 if (playerrace=="Hispanic or Latinx")
replace temp=4 if (playerrace=="Asian")
replace temp=. if subsessionround_number~=110
bysort id: egen race=mean(temp)
drop temp


*for constructing demographic features
gen temp=(playerrace=="White")
replace temp=. if subsessionround_number~=110
bysort id: egen race_white=mean(temp)
drop temp
gen temp=(playerrace=="Asian")
replace temp=. if subsessionround_number~=110
bysort id: egen race_asian=mean(temp)
drop temp
gen temp=(playerincome=="$0 to $25,000")
replace temp=. if subsessionround_number~=110
bysort id: egen income_0=mean(temp)
drop temp
gen temp=(playerincome=="$25,001 to $50,000")
replace temp=. if subsessionround_number~=110
bysort id: egen income_25=mean(temp)
drop temp
gen temp=((playereducation=="College degree")|(playereducation=="Graduate or professional degree"))
replace temp=. if subsessionround_number~=110
bysort id: egen education_high=mean(temp)
drop temp
gen temp=playerage
replace temp=. if subsessionround_number~=110
bysort id: egen age=mean(temp)
drop temp


*remove questionaire data sequence
drop if subsessionround_number==110


*define after tax income
replace playerafter_tax_income=playertaxable_income-playertax_paid if sessiontype>=11
*add capital gains taxes to tax payed
replace playertax_paid=playertax_paid+playercapital_gains_tax_paid if sessiontype==3



*change the unit
*in dollar 
*cumulative payoff each sequence assuming has not ended
replace playertotal_payoff=playertotal_payoff/100
*period payoff 
replace playerpayoff=playerpayoff/100 

*in thousand
replace playerconsumption=playerconsumption/1000
replace playerbalance_RA=playerbalance_RA/1000
replace playerbalance_TDA=playerbalance_TDA/1000
replace playerbalance_CA=playerbalance_CA/1000
replace playertaxable_income=playertaxable_income /1000
replace  playertax_paid=playertax_paid/1000
replace playerafter_tax_income=playerafter_tax_income/1000
*different variables are used to record contribution amount in T4-T6
replace playercontributionTDA=0 if playercontributionTDA==.
replace playercontributionCA=0 if playercontributionCA==.
replace playercontributionTDA=playercontributionTDA/1000
replace playercontributionCA=playercontributionCA/1000
replace playercontribution=playercontribution/1000
replace playercontribution=playercontributionTDA if sessiontype==4 
replace playercontribution=playercontributionCA if sessiontype==5 | sessiontype==6
replace playercontribution=0 if playercontribution==.

replace playerendowment=playerendowment/1000


*average and mean TDA balance at period 15
sum playerbalance_TDA if subsessionround_in_sequence==15 & sessiontype==1, d
sum playerbalance_TDA if subsessionround_in_sequence==15 & sessiontype==4, d
sum playerbalance_CA if subsessionround_in_sequence==15 & sessiontype==5, d
sum playerbalance_CA if subsessionround_in_sequence==15 & sessiontype==6, d


*net worth
replace playerbalance_TDA=0 if playerbalance_TDA==.
replace playerbalance_CA=0 if  playerbalance_CA==.

gen networth=playerbalance_RA+playerbalance_TDA+playerbalance_CA

*find out CA account usage
sum playerbalance_CA if subsessionround_in_sequence==15 & sessiontype==5, d
sum playerbalance_CA if subsessionround_in_sequence==15 & sessiontype==6, d
list id if playerbalance_CA==0 & subsessionround_in_sequence==15 & sessiontype==5
list id if playerbalance_CA==0 & subsessionround_in_sequence==15 & sessiontype==6




tsset id_sequence subsessionround_in_sequence
*calculate total tax savings from TDA (both contribution and accumulated interests)
gen playertaxable_income_before=playertaxable_income+playercontribution if (sessiontype==1 | sessiontype==4 | sessiontype==11) & subsessionround_in_sequence==1
replace playertaxable_income_before=playertaxable_income+playercontribution+L.playerbalance_TDA*0.1 if (sessiontype==1 | sessiontype==4 | sessiontype==11) & subsessionround_in_sequence>1
gen playertax_paid_before=. 
replace playertax_paid_before=0 if (sessiontype==1 | sessiontype==4 | sessiontype==11)
replace playertax_paid_before=0.1*(playertaxable_income_before-21) if (sessiontype==1 | sessiontype==4 | sessiontype==11) & playertaxable_income_before>21
replace playertax_paid_before=0.15*(playertaxable_income_before-39)+1.8 if (sessiontype==1 | sessiontype==4 | sessiontype==11) & playertaxable_income_before>39
replace playertax_paid_before=0.25*(playertaxable_income_before-96)+10.35 if (sessiontype==1 | sessiontype==4 | sessiontype==11) & playertaxable_income_before>96
gen tax_exp=.
replace tax_exp=playertax_paid_before-playertax_paid

*calculate tax savings from TDA contribution alone
gen temp1=playertaxable_income+playercontribution if (sessiontype==1 | sessiontype==4 | sessiontype==11) 
gen temp2=. 
replace temp2=0 if (sessiontype==1 | sessiontype==4 | sessiontype==11)
replace temp2=0.1*(temp1-21) if (sessiontype==1 | sessiontype==4 | sessiontype==11) & temp1>21
replace temp2=0.15*(temp1-39)+1.8 if (sessiontype==1 | sessiontype==4 | sessiontype==11) & temp1>39
replace temp2=0.25*(temp1-96)+10.35 if (sessiontype==1 | sessiontype==4 | sessiontype==11) & temp1>96
gen tdactaxsave=temp2-playertax_paid
replace tdactaxsave=0 if tdactaxsave==.

*total savings
gen totalsaving=playerafter_tax_income-playerconsumption
replace totalsaving=playercontribution-tdactaxsave+totalsaving if (sessiontype==1 | sessiontype==4 | sessiontype==11) 

drop temp1 temp2

replace totalsaving=. if subsessionround_in_sequence>15

*revised after tax income, adding TDA contribution and deducting tax savings on TDA contribution 
gen revised_after_tax_income=playerafter_tax_income
replace revised_after_tax_income=playerafter_tax_income+playercontribution-tdactaxsave if (sessiontype==1 | sessiontype==4 | sessiontype==11) 

*contribution rate
gen contribution2endowment=playercontribution/playerendowment
replace contribution2endowment=. if subsessionround_in_sequence>15
gen contribution2aftertaxinc=playercontribution/revised_after_tax_income
replace contribution2aftertaxinc=. if subsessionround_in_sequence>15
replace contribution2endowment=. if (sessiontype==2) |   (sessiontype==3) | (sessiontype==12) | (sessiontype==13)   
replace contribution2aftertaxinc=. if (sessiontype==2) |   (sessiontype==3) | (sessiontype==12) | (sessiontype==13)   
*total saving rate
gen totalsaving2endowment=totalsaving/playerendowment
gen totalsaving2aftertaxinc=totalsaving/revised_after_tax_income

sum contribution2endowment if sessiontype==5
sum contribution2endowment if sessiontype==6

*generate revised TDA balances b* 
*assuming subjects pay taxes on TDA contribution and accumulated interests. 
gen revised_playerbalance_TDA=0
replace revised_playerbalance_TDA=playerbalance_TDA-tax_exp if subsessionround_in_sequence==1 &  (sessiontype==1 | sessiontype==4 | sessiontype==11)
foreach i of numlist 2/35{
gen temp1=playertaxable_income+playercontribution+L.revised_playerbalance_TDA*0.1 if (sessiontype==1 | sessiontype==4 | sessiontype==11) & subsessionround_in_sequence==`i'
gen temp2=. 
replace temp2=0 if (sessiontype==1 | sessiontype==4 | sessiontype==11)
replace temp2=0.1*(temp1-21) if (sessiontype==1 | sessiontype==4 | sessiontype==11) & temp1>21
replace temp2=0.15*(temp1-39)+1.8 if (sessiontype==1 | sessiontype==4 | sessiontype==11) & temp1>39
replace temp2=0.25*(temp1-96)+10.35 if (sessiontype==1 | sessiontype==4 | sessiontype==11) & temp1>96
replace revised_playerbalance_TDA=L.revised_playerbalance_TD*1.1+playercontribution-(temp2-playertax_paid) if subsessionround_in_sequence==`i' &  (sessiontype==1 | sessiontype==4 | sessiontype==11) & playertaxable_income>0
drop temp1 temp2
}



*ending round for each session
bysort id_sequence: egen endinground=total(subsessionpaid_round) if  subsessionround_in_sequence>=1 & subsessionround_in_sequence<=35 




*theretical survival rate for each model period (the probabiltiy each period is the last period)
gen survprob=1
foreach i of numlist 16/35{
replace survprob=(0.9)^(`i'-16) if subsessionround_in_sequence==`i'
}

sort sessiontype session participantid_in_session subsessionsequence subsessionround_in_sequence


*generate theory predicted consumption levels
gen temp=playerconsumption if sessiontype>=10
bysort treatment subsessionround_in_sequence: egen consumption_theory=mean(temp)
drop temp
*deviation measures
gen consumption_dev=playerconsumption-consumption_theory
gen con_dev2=(playerconsumption-consumption_theory)^2
gen con_per_dev2=((playerconsumption-consumption_theory)/consumption_theory)^2*100


*5 period average values: 1 periods 1-5, 6 p 6-10, 11 p11-15, 16 p16-20, 21 p21-25 26 p25-30, 31 p31-35
global varlist periodlist="1 6 11 16 21"
foreach i of numlist 1(5)31{
bysort id_sequence : egen mse`i'=mean(con_dev2) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
bysort id_sequence : egen mspe`i'=mean(con_per_dev2) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
bysort id_sequence : egen con`i'=mean(playerconsumption) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
bysort id_sequence : egen networth`i'=mean(networth) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
}

*first 25 period average values
bysort id_sequence : egen mse25=mean(con_dev2) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=25
bysort id_sequence : egen mspe25=mean(con_per_dev2) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=25
bysort id_sequence : egen con25=mean(playerconsumption) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=25
bysort id_sequence : egen networth25=mean(networth) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=25

*five period average
foreach i of numlist 1(5)11{
bysort id_sequence : egen totalsaving2endowment`i'=mean(totalsaving2endowment) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
bysort id_sequence : egen totalsaving2aftertaxinc`i'=mean(totalsaving2aftertaxinc) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
bysort id_sequence : egen contribution2endowment`i'=mean(contribution2endowment) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
bysort id_sequence : egen contribution2aftertaxinc`i'=mean(contribution2aftertaxinc) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
bysort id_sequence : egen after_tax_income`i'=mean(playerafter_tax_income) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
bysort id_sequence : egen tax_paid`i'=mean(playertax_paid) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
bysort id_sequence : egen revised_after_tax_income`i'=mean(revised_after_tax_income) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
bysort id_sequence : egen tax_paid_2_endowment`i'=mean(playertax_paid/playerendowment) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'

}


*average of first 15 periods

bysort id_sequence : egen totalsaving2endowment15=mean(totalsaving2endowment) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=15
bysort id_sequence : egen totalsaving2aftertaxinc15=mean(totalsaving2aftertaxinc) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=15
bysort id_sequence : egen contribution2endowment15=mean(contribution2endowment) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=15
bysort id_sequence : egen contribution2aftertaxinc15=mean(contribution2aftertaxinc) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=15


*maximum number of rounds with decision data
bysort id_sequence: egen maxround=max(subsessionround_in_sequence) 

* 35 period average for the subsample with valid data for all 35 periods
bysort id_sequence : egen mse35=mean(con_dev2) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=35 & maxround==35
bysort id_sequence : egen mspe35=mean(con_per_dev2) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=35 & maxround==35
bysort id_sequence : egen con35=mean(playerconsumption) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=35 & maxround==35
bysort id_sequence : egen networth35=mean(networth) if subsessionround_in_sequence>=1 & subsessionround_in_sequence<=35 & maxround==35

*define per period payoff for model 
tsset id_sequence subsessionround_in_sequence
foreach j of numlist 11/13{
	replace playerpayoff= playertotal_payoff if  subsessionround_in_sequence==1 & sessiontype==`j'
	foreach i of numlist 2/35{
	replace playerpayoff= playertotal_payoff-L.playertotal_payoff if  subsessionround_in_sequence==`i'	 &  sessiontype==`j'
	}
}

*total payoff for 5 periods weighted by survival prob
foreach i of numlist 1(5)31{
bysort id_sequence : egen payoff`i'=total(playerpayoff*survprob) if subsessionround_in_sequence>=`i' & subsessionround_in_sequence<`=`i'+5'
}

*payoff from the first 25 periods (only)
bysort id_sequence: egen payoff24=total(playerpayoff*survprob) if  subsessionround_in_sequence>=1 & subsessionround_in_sequence<=25
*lifetime utility, add the average payoff for the last 10 periods for subjects who end at period 25
bysort id_sequence: egen payoff25=total(playerpayoff*survprob) if  subsessionround_in_sequence>=1 & subsessionround_in_sequence<=35
foreach i of numlist 1/6{
	sum payoff26 if sessiontype==`i'
	replace payoff25=payoff25+r(mean) if sessiontype==`i' & maxround==25
	sum payoff31 if sessiontype==`i'
	replace payoff25=payoff25+r(mean) if sessiontype==`i' & maxround==25
	
}


*=============================
*control variables
*=============================
gen subsessionsequence2=(subsessionsequence==2)
global control="crt financialliteracy riskaverse female subsessionsequence2"


*summary statistics
local varlist="female crt financialliteracy riskaverse participantpayoff"
estpost summ `varlist' if subsessionround_in_sequence==1 & (sessiontype<10) & subsessionsequence==1, d
estout using "output/summary.xls",  cells("count mean sd p10 p25 p50 p75 p90") title("all") replace 

foreach i of numlist 1/6{
estpost summ `varlist' if subsessionround_in_sequence==1 & sessiontype==`i' & subsessionsequence==1, d
estout using "output/summary.xls",  cells("count mean sd p10 p25 p50 p75 p90") title("T`i'") append
}


* more summary statistics about demographic variables

local varlist2="female age race_white race_asian income_0 income_25 education_high crt financialliteracy riskaverse participantpayoff"

estpost summ `varlist2' if subsessionround_in_sequence==1 & sessiontype<10 & subsessionsequence==1, d
estout using "output/summary.xls",  cells("count mean sd p10 p25 p50 p75 p90") title("all alternative statistics") append

*level difference across treatments and statistical significance for control variables

local varlist="female crt financialliteracy riskaverse participantpayoff"

foreach stub of local varlist{
reg `stub'  $treatmentlist  if subsessionround_in_sequence==1 & subsessionsequence==1 & sessiontype<10, robust
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar pvalue`m'`k' r(p)
} 
}
est store `stub'
}

esttab `varlist' using "output/summary_diff.csv", /*
*/  keep($treatmentlist)  replace  cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) star( * 0.10 ** 0.05 *** 0.010)  /*
*/ stats(r2_a N diff32 pvalue32 diff42 pvalue42 diff52 pvalue52 diff62 pvalue62 diff43 pvalue43 diff53 pvalue53 diff63 pvalue63 diff54 pvalue54 diff64 pvalue64 diff65 pvalue65 , fmt(%10.4f %9.0g) labels(R-squared))  collabels(none) title("difference")


*=============================
*regression analysis
*the number of regressions are more than that reported in the paper.
*regressions used to construct tables reported in the paper and appendix are noted
*each regression result includes 
*the estimated coefficient const and t2-t6
*the treatment difference diffxx and sdxx
*the difference in theory predictions mean21 mean31 mean32
*=============================

*mean squared percent errors 
est clear
foreach i of numlist 1 (5)21 25 26 31 35{
reg mspe`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
est store mspe`i'
}


esttab mspe* using "output/result.csv", /*
*/  keep(_cons $treatmentlist $control) replace cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) star( * 0.10 ** 0.05 *** 0.010)  /*
*/ stats(r2_a N diff32 sd32 diff42 sd42 diff52 sd52 diff62 sd62 diff43 sd43 diff53 sd53 diff63 sd63 diff54 sd54 diff64 sd64 diff65 sd65  , fmt(%9.2f %9.0g ) labels(R-squared N T3-T2 sd T4-T2 sd T5-T2 sd T6-T2 sd T4-T3 sd T5-T3 sd T6-T3 sd T5-T4 sd T6-T4 sd T6-T5 sd))  collabels(none) title(" mspe")

est clear

*mean square erros 
foreach i of numlist 1 (5)21 25 26 31 35{
reg mse`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
est store mse`i'
}

esttab mse* using "output/result.csv", /*
*/  keep(_cons $treatmentlist $control) append cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) star( * 0.10 ** 0.05 *** 0.010)  /*
*/ stats(r2_a N diff32 sd32 diff42 sd42 diff52 sd52 diff62 sd62 diff43 sd43 diff53 sd53 diff63 sd63 diff54 sd54 diff64 sd64 diff65 sd65  , fmt(%9.2f %9.0g ) labels(R-squared N T3-T2 sd T4-T2 sd T5-T2 sd T6-T2 sd T4-T3 sd T5-T3 sd T6-T3 sd T5-T4 sd T6-T4 sd T6-T5 sd))  collabels(none) title(" mse")

*contribution rate
est clear
 foreach i of numlist 1(5)11 15{
reg contribution2endowment`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
est store contribution2endowment`i'
}


*saving rate
 foreach i of numlist 1(5)11 15{
reg totalsaving2endowment`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
foreach j of numlist 1/3{
sum totalsaving2endowment`i' if subsessionround_in_sequence==`i' & sessiontype==`=`j'+10'
local mean`j'=r(mean)
}
estadd scalar mean21=`mean2'-`mean1'
estadd scalar mean31=`mean3'-`mean1'
estadd scalar mean32=`mean3'-`mean2'

est store totalsaving2endowment`i'
}





esttab contribution2endowment* totalsaving2endowment* using "output/result.csv", /*
*/  keep(_cons $treatmentlist $control) append cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) star( * 0.10 ** 0.05 *** 0.010)  /*
*/ stats(r2_a N diff32 sd32 diff42 sd42 diff52 sd52 diff62 sd62 diff43 sd43 diff53 sd53 diff63 sd63 diff54 sd54 diff64 sd64 diff65 sd65  mean21 mean31 mean32 , fmt(%9.2f %9.0g ) labels(R-squared N T3-T2 sd T4-T2 sd T5-T2 sd T6-T2 sd T4-T3 sd T5-T3 sd T6-T3 sd T5-T4 sd T6-T4 sd T6-T5 sd T2-T1 T3-T1 T3-T2))  collabels(none) title("to endowment ratio")

est clear


*alternative measures of contribution and saving rates
 foreach i of numlist 1(5)11 15{
reg contribution2aftertaxinc`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
est store contribution2aftertaxinc`i'

}




 foreach i of numlist 1(5)11 15{
reg totalsaving2aftertaxinc`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
foreach j of numlist 1/3{
sum totalsaving2aftertaxinc`i' if subsessionround_in_sequence==`i' & sessiontype==`=`j'+10'
local mean`j'=r(mean)
}
estadd scalar mean21=`mean2'-`mean1'
estadd scalar mean31=`mean3'-`mean1'
estadd scalar mean32=`mean3'-`mean2'

est store totalsaving2aftertaxinc`i'

}


esttab contribution2aftertaxinc* totalsaving2aftertaxinc* using "output/result.csv", /*
*/  keep(_cons $treatmentlist $control) append cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) star( * 0.10 ** 0.05 *** 0.010)  /*
*/ stats(r2_a N diff32 sd32 diff42 sd42 diff52 sd52 diff62 sd62 diff43 sd43 diff53 sd53 diff63 sd63 diff54 sd54 diff64 sd64 diff65 sd65  mean21 mean31 mean32 , fmt(%9.2f %9.0g ) labels(R-squared N T3-T2 sd T4-T2 sd T5-T2 sd T6-T2 sd T4-T3 sd T5-T3 sd T6-T3 sd T5-T4 sd T6-T4 sd T6-T5 sd T2-T1 T3-T1 T3-T2))  collabels(none) title(" to after tax inc ratio")


*consumption levels
 est clear
foreach i of numlist 1(5)21 25 26 31 35{
reg con`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
foreach j of numlist 1/3{
sum con`i' if subsessionround_in_sequence==`i' & sessiontype==`=`j'+10'
local mean`j'=r(mean)
}
estadd scalar mean21=`mean2'-`mean1'
estadd scalar mean31=`mean3'-`mean1'
estadd scalar mean32=`mean3'-`mean2'
est store con`i'
}

esttab con* using "output/result.csv", /*
*/  keep(_cons $treatmentlist $control) append cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) star( * 0.10 ** 0.05 *** 0.010)  /*
*/ stats(r2_a N diff32 sd32 diff42 sd42 diff52 sd52 diff62 sd62 diff43 sd43 diff53 sd53 diff63 sd63 diff54 sd54 diff64 sd64 diff65 sd65  mean21 mean31 mean32 , fmt(%9.2f %9.0g ) labels(R-squared N T3-T2 sd T4-T2 sd T5-T2 sd T6-T2 sd T4-T3 sd T5-T3 sd T6-T3 sd T5-T4 sd T6-T4 sd T6-T5 sd T2-T1 T3-T1 T3-T2))  collabels(none) title(" consumption")

*net worth
 est clear
foreach i of numlist 1(5)21 25 26 31 35{
reg networth`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
foreach j of numlist 1/3{
sum networth`i' if subsessionround_in_sequence==`i' & sessiontype==`=`j'+10'
local mean`j'=r(mean)
}
estadd scalar mean21=`mean2'-`mean1'
estadd scalar mean31=`mean3'-`mean1'
estadd scalar mean32=`mean3'-`mean2'
est store networth`i'
}

esttab networth* using "output/result.csv", /*
*/  keep(_cons $treatmentlist $control) append cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) star( * 0.10 ** 0.05 *** 0.010)  /*
*/ stats(r2_a N diff32 sd32 diff42 sd42 diff52 sd52 diff62 sd62 diff43 sd43 diff53 sd53 diff63 sd63 diff54 sd54 diff64 sd64 diff65 sd65  mean21 mean31 mean32 , fmt(%9.2f %9.0g ) labels(R-squared N T3-T2 sd T4-T2 sd T5-T2 sd T6-T2 sd T4-T3 sd T5-T3 sd T6-T3 sd T5-T4 sd T6-T4 sd T6-T5 sd T2-T1 T3-T1 T3-T2))  collabels(none) title(" networth")


*average 5 period payoff, lifetime utility payoff25, cumulative payment for first 25 period payoff24
foreach i of numlist 1(5)21 24 26 31 25 {
reg payoff`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
foreach j of numlist 1/3{
sum payoff`i' if subsessionround_in_sequence==`i' & sessiontype==`=`j'+10'
local mean`j'=r(mean)
}
estadd scalar mean21=`mean2'-`mean1'
estadd scalar mean31=`mean3'-`mean1'
estadd scalar mean32=`mean3'-`mean2'

est store payoff`i'
}


esttab payoff* using "output/result.csv", /*
*/  keep(_cons $treatmentlist $control) append cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) star( * 0.10 ** 0.05 *** 0.010)  /*
*/ stats(r2_a N diff32 sd32 diff42 sd42 diff52 sd52 diff62 sd62 diff43 sd43 diff53 sd53 diff63 sd63 diff54 sd54 diff64 sd64 diff65 sd65  mean21 mean31 mean32, fmt(%9.2f %9.2f %9.2f %9.0g %9.2f) labels( R-squared N T3-T2 sd T4-T2 sd T5-T2 sd T6-T2 sd T4-T3 sd T5-T3 sd T6-T3 sd T5-T4 sd T6-T4 sd T6-T5 sd T2-T1 T3-T1 T3-T2))  collabels(none) title(" payoff")

est clear

*=============================
*Figure 4
*=============================

gen coef_time=.
gen coef_t2=.
gen coef_lb_t2=.
gen coef_ub_t2=.
gen coef_t3=.
gen coef_lb_t3=.
gen coef_ub_t3=.
gen coef_t4=.
gen coef_lb_t4=.
gen coef_ub_t4=.
gen coef_t5=.
gen coef_lb_t5=.
gen coef_ub_t5=.
gen coef_t6=.
gen coef_lb_t6=.
gen coef_ub_t6=.
foreach i of numlist 16/35 {
reg playertotal_payoff  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
replace coef_time=`i' in `i'
replace coef_t2=_b[t2] in `i'
replace coef_lb_t2=_b[t2]-1.96*_se[t2] in `i'
replace coef_ub_t2=_b[t2]+1.96*_se[t2] in `i'
replace coef_t3=_b[t3] in `i'
replace coef_lb_t3=_b[t3]-1.96*_se[t3] in `i'
replace coef_ub_t3=_b[t3]+1.96*_se[t3] in `i'
replace coef_t4=_b[t4] in `i'
replace coef_lb_t4=_b[t4]-1.96*_se[t4] in `i'
replace coef_ub_t4=_b[t4]+1.96*_se[t4] in `i'
replace coef_t5=_b[t5] in `i'
replace coef_lb_t5=_b[t5]-1.96*_se[t5] in `i'
replace coef_ub_t5=_b[t5]+1.96*_se[t5] in `i'
replace coef_t6=_b[t6] in `i'
replace coef_lb_t6=_b[t6]-1.96*_se[t6] in `i'
replace coef_ub_t6=_b[t6]+1.96*_se[t6] in `i'
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
foreach j of numlist 1/3{
sum playertotal_payoff if subsessionround_in_sequence==`i' & sessiontype==`=`j'+10'
local mean`j'=r(mean)
}
estadd scalar mean21=`mean2'-`mean1'
estadd scalar mean31=`mean3'-`mean1'
estadd scalar mean32=`mean3'-`mean2'
est store totalpayoff`i'
}

esttab totalpayoff* using "output/result.csv", /*
*/  keep(_cons $treatmentlist $control) append cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) star( * 0.10 ** 0.05 *** 0.010)  /*
*/ stats( r2_a N diff32 sd32 diff42 sd42 diff52 sd52 diff62 sd62 diff43 sd43 diff53 sd53 diff63 sd63 diff54 sd54 diff64 sd64 diff65 sd65  mean21 mean31 mean32, fmt(%9.2f %9.2f %9.2f %9.0g %9.2f) labels(R-squared N T3-T2 sd T4-T2 sd T5-T2 sd T6-T2 sd T4-T3 sd T5-T3 sd T6-T3 sd T5-T4 sd T6-T4 sd T6-T5 sd T2-T1 T3-T1 T3-T2))  collabels(none) title("cumulative payoff")



preserve
keep if coef_time>=16 & coef_time<=25
twoway (connected coef_t2 coef_time, color(red) lwidth(1)) (rcap coef_lb_t2 coef_ub_t2 coef_time, color(red)) /*
*/  (connected coef_t3 coef_time, color(green) lwidth(0.2) msymbol(S)) (rcap coef_lb_t3 coef_ub_t3 coef_time, color(green)) /*
 */ (connected coef_t4 coef_time, color(blue) lp(dash_dot) lwidth(2)) (rcap coef_lb_t4 coef_ub_t4 coef_time, color(blue)) /*
  */, ysc(range(-2.5 4)) ylabel(-2 0 2 4) yline(0, lstyle(refline) lcolor(black))  legend(order(1 "T2" 3 "T3" 5 "T4") row(1)) xtitle("Period") ytitle("Payoff")  saving(graph/paymentT1T3, replace)
  graph export graph/paymentT1T3.pdf, replace
  twoway  /*
 */ (connected coef_t5 coef_time, color(red) lwidth(1) lp(dash_dot) )  (rcap coef_lb_t5 coef_ub_t5 coef_time) /*
  */ (connected coef_t6 coef_time, color(red) lwidth(1) lp(shortdash) msymbol(S)) (rcap coef_lb_t6 coef_ub_t6 coef_time)  /*
  */, yline(0, lstyle(refline) lcolor(black))  ysc(range(-2.5 4)) ylabel(-2 0 2 4) legend(order(1 "T5" 3 "T6" ) row(1)) xtitle("Period") ytitle("Payoff")  saving(graph/paymentT5T6, replace)
  graph export graph/paymentT5T6.pdf, replace  
restore

est clear

*tax liablity 
 foreach i of numlist 1(5)11 {
reg tax_paid`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
foreach j of numlist 1/3{
sum tax_paid`i'  if subsessionround_in_sequence==`i' & sessiontype==`=`j'+10'
local mean`j'=r(mean)
}
estadd scalar mean21=`mean2'-`mean1'
estadd scalar mean31=`mean3'-`mean1'
estadd scalar mean32=`mean3'-`mean2'
est store tax_paid`i'
}

*tax liablity to endowment ratio
 foreach i of numlist 1(5)11 {
reg tax_paid_2_endowment`i'  $treatmentlist $control if subsessionround_in_sequence==`i' & sessiontype<10, vce(cluster id)
foreach k of numlist 2/5{
	foreach m of numlist `=`k'+1'/6{
lincom t`m'-t`k'
estadd scalar diff`m'`k' r(estimate)
estadd scalar sd`m'`k' r(se)
} 
}
foreach j of numlist 1/3{
sum tax_paid_2_endowment`i'  if subsessionround_in_sequence==`i' & sessiontype==`=`j'+10'
local mean`j'=r(mean)
}
estadd scalar mean21=`mean2'-`mean1'
estadd scalar mean31=`mean3'-`mean1'
estadd scalar mean32=`mean3'-`mean2'

est store tax_paid_2_endowment`i'
}

esttab tax_paid*  using "output/result.csv", /*
*/  keep(_cons $treatmentlist $control) append cells(b(star fmt(%9.2f)) se(par fmt(%9.2f))) star( * 0.10 ** 0.05 *** 0.010)  /*
*/ stats(r2_a N diff32 sd32 diff42 sd42 diff52 sd52 diff62 sd62 diff43 sd43 diff53 sd53 diff63 sd63 diff54 sd54 diff64 sd64 diff65 sd65  mean21 mean31 mean32, fmt(%9.2f %9.0g ) labels(R-squared N T3-T2 sd T4-T2 sd T5-T2 sd T6-T2 sd T4-T3 sd T5-T3 sd T6-T3 sd T5-T4 sd T6-T4 sd T6-T5 sd T2-T1 T3-T1 T3-T2))  collabels(none) title("tax paid ")




gen revised_networth=playerbalance_RA+revised_playerbalance_TDA+playerbalance_CA
replace revised_networth=networth if sessiontype~=1 & sessiontype~=4 & sessiontype~=11

*decomposing the difference in retirement wealth across treatments

gen level=.
gen se=.

gen level_model=0

reg networth t1 if subsessionround_in_sequence==15 & (sessiontype==1 | sessiontype==2), vce(cluster id)
replace level=_b[t1] in 1
replace se=_se[t1] in 1
reg networth t1 if subsessionround_in_sequence==15 & (sessiontype==1 | sessiontype==4), vce(cluster id)
replace level=_b[t1] in 2
replace se=_se[t1] in 2
reg revised_networth t4 if subsessionround_in_sequence==15 & (sessiontype==4 | sessiontype==5), vce(cluster id)
replace level=_b[t4] in 4
replace se=_se[t4] in 4
reg networth t5 if subsessionround_in_sequence==15 & (sessiontype==5 | sessiontype==6), vce(cluster id)
replace level=_b[t5] in 5
replace se=_se[t5] in 5
reg networth t6 if subsessionround_in_sequence==15 & (sessiontype==6 | sessiontype==2) ,vce(cluster id)
replace level=_b[t6] in 6
replace se=_se[t6] in 6
preserve
keep if subsessionround_in_sequence==15 & sessiontype==4 
keep networth revised_networth id_sequence id
rename networth networth1
rename revised_networth networth2
reshape long networth, i(id id_sequence) j(original)
replace original=0 if original==2
reg networth original , vce(cluster id)
local temp1=_b[original]
local temp2=_se[original]
restore
replace level=`temp1' in 3
replace se=`temp2' in 3

sum networth  if subsessionround_in_sequence==15 & sessiontype==11
local t1mean=r(mean)
sum revised_networth  if subsessionround_in_sequence==15 & sessiontype==11
local revised_t1mean=r(mean)
sum networth  if subsessionround_in_sequence==15 & sessiontype==12
local t2mean=r(mean)

replace level_model=0
replace level_model=`t1mean'-`t2mean' in 1
replace level_model=`t1mean'-`revised_t1mean' in 3
replace level_model=`revised_t1mean'-`t2mean' in 4

br level se level_model




 
preserve
global varlist=" playertotal_payoff playerconsumption playerbalance_RA playerbalance_TDA playerbalance_CA playercontribution revised_playerbalance_TDA "
collapse $varlist , by(sessiontype subsessionround_in_sequence)
drop if subsessionround_in_sequence==55 /*record the final questionnaire*/
replace playerbalance_TDA=0 if playerbalance_TDA==.
replace playerbalance_CA=0 if playerbalance_CA==.
replace revised_playerbalance_TDA=0 if revised_playerbalance_TDA==.

gen networth=playerbalance_RA+playerbalance_TDA+playerbalance_CA
gen revised_networth=playerbalance_RA+revised_playerbalance_TDA+playerbalance_CA
replace revised_networth=networth if sessiontype~=1 & sessiontype~=4 & sessiontype~=11
global varlist="playerconsumption networth "
label var playertotal_payoff "Cumulative earnings"
label var playerconsumption "Consumption (000)"
label var  networth "Total networth (000)"
label var playerbalance_RA "RA balance"
label var playerbalance_TDA "TDA balance (000)"
label var subsessionround_in_sequence "Period"
label var  revised_networth "Revised total networth (000)"
keep if subsessionround_in_sequence<=25 /*record the final questionnaire*/





*=============================
*Figure 1
*=============================

twoway (line playerconsumption subsessionround_in_sequence if sessiontype==11 ,  lcolor(blue) ) /*
*/ (line playerconsumption subsessionround_in_sequence if sessiontype==12,  lp(shortdash) lcolor(red) ) /*
*/  (line playerconsumption subsessionround_in_sequence if sessiontype==13,  lp(longdash) lcolor(green) ) /*
*/  (connected playerconsumption subsessionround_in_sequence if sessiontype==11,  lcolor(blue) msymbol(X) mcolor(blue) ) /*
*/, xline(16) legend(order(1 "T1/T4 model" 2 "T2/T5/T6 model" 3 "T3 model" 4 "T1*/T4* model" ) rows(1) size(*0.8) symxsize(*0.8))   name(consumption, replace)
graph save graph/consumption_model, replace
graph export graph/consumption_model.pdf, replace


twoway (line networth subsessionround_in_sequence if sessiontype==11 ,  lcolor(blue) ) /*
*/ (line networth subsessionround_in_sequence if sessiontype==12,  lp(shortdash) lcolor(red) ) /*
*/  (line networth subsessionround_in_sequence if sessiontype==13,  lp(longdash) lcolor(green) ) /*
*/  (connected revised_networth subsessionround_in_sequence if sessiontype==11,  lcolor(blue) msymbol(X) mcolor(blue) ) /*
*/, xline(16) legend(order(1 "T1/T4 model" 2 "T2/T5/T6 model" 3 "T3 model" 4 "T1*/T4* model" ) rows(1) size(*0.8) symxsize(*0.8))   name(networth, replace) ytitle("Net worth (000)")
graph save graph/networth_model, replace


grc1leg consumption networth
graph export graph/model.pdf, replace

*=============================
*Figure 2
*=============================

foreach stub of global varlist{
twoway (line `stub' subsessionround_in_sequence if sessiontype==1 ,lcolor(blue) lwidth(2) ) /*
*/(line `stub' subsessionround_in_sequence if sessiontype==2,  lcolor(red) lwidth(1) ) /*
*/ (line `stub' subsessionround_in_sequence if sessiontype==3,  lcolor(green) lwidth(0.2) ) /*
*/ (line `stub' subsessionround_in_sequence if sessiontype==4 , lp(dash_dot) lcolor(blue) lwidth(2) ) /*
*/ (line `stub' subsessionround_in_sequence if sessiontype==5 , lp(dash_dot) lcolor(red) lwidth(1) ) /*
*/ (line `stub' subsessionround_in_sequence if sessiontype==6 , lp(shortdash) lcolor(red) lwidth(1) ) /*
*/(line `stub' subsessionround_in_sequence if sessiontype==11 , lp(dot) lcolor(blue) lwidth(2)) /*
*/ (line `stub' subsessionround_in_sequence if sessiontype==12, lp(dot) lcolor(red) lwidth(1)) /*
*/  (line `stub' subsessionround_in_sequence if sessiontype==13, lp(dot) lcolor(green) lwidth(0.2)) /*
*/, xline(16) legend(order(1 "T1" 2 "T2" 3 "T3" 4 "T4"  5 "T5" 6 "T6" 7 "T1/T4 model" 8 "T2/T5/T6 model" 9 "T3 model") rows(3) size(*0.8) symxsize(*0.8))   name(`stub', replace)
graph save graph/`stub', replace
graph export graph/`stub'.pdf, replace
}

grc1leg playerconsumption networth
graph export graph/meancomparison.pdf, replace


restore



*=============================
*App Figure C3 (a)
*=============================

preserve
global varlist=" playertotal_payoff playerconsumption playerbalance_RA playerbalance_TDA playerbalance_CA playercontribution  revised_playerbalance_TDA "
collapse (median) $varlist , by(sessiontype subsessionround_in_sequence)
**# Bookmark #1
drop if subsessionround_in_sequence==55 /*record the final questionnaire*/
replace playerbalance_TDA=0 if playerbalance_TDA==.
replace playerbalance_CA=0 if playerbalance_CA==.
replace revised_playerbalance_TDA=0 if revised_playerbalance_TDA==.

gen networth=playerbalance_RA+playerbalance_TDA+playerbalance_CA
gen revised_networth=playerbalance_RA+revised_playerbalance_TDA+playerbalance_CA
replace revised_networth=networth if sessiontype~=1 & sessiontype~=4 & sessiontype~=11
global varlist="playerconsumption networth "
label var playertotal_payoff "Cumulative earnings"
label var playerconsumption "Consumption (000)"
label var  networth "Total networth (000)"
label var playerbalance_RA "RA balance"
label var playerbalance_TDA "TDA balance (000)"
label var subsessionround_in_sequence "Period"
label var  revised_networth "Revised total networth (000)"
keep if subsessionround_in_sequence<=25 /*record the final questionnaire*/



foreach stub of global varlist{
twoway (line `stub' subsessionround_in_sequence if sessiontype==1 ,lcolor(blue) lwidth(2) ) /*
*/(line `stub' subsessionround_in_sequence if sessiontype==2,  lcolor(red) lwidth(1) ) /*
*/ (line `stub' subsessionround_in_sequence if sessiontype==3,  lcolor(green) lwidth(0.2) ) /*
*/ (line `stub' subsessionround_in_sequence if sessiontype==4 , lp(dash_dot) lcolor(blue) lwidth(2) ) /*
*/ (line `stub' subsessionround_in_sequence if sessiontype==5 , lp(dash_dot) lcolor(red) lwidth(1) ) /*
*/ (line `stub' subsessionround_in_sequence if sessiontype==6 , lp(shortdash) lcolor(red) lwidth(1) ) /*
*/(line `stub' subsessionround_in_sequence if sessiontype==11 , lp(dot) lcolor(blue) lwidth(2)) /*
*/ (line `stub' subsessionround_in_sequence if sessiontype==12, lp(dot) lcolor(red) lwidth(1)) /*
*/  (line `stub' subsessionround_in_sequence if sessiontype==13, lp(dot) lcolor(green) lwidth(0.2)) /*
*/, xline(16) legend(order(1 "T1" 2 "T2" 3 "T3" 4 "T4"  5 "T5" 6 "T6" 7 "T1/T4 model" 8 "T2/T5/T6 model" 9 "T3 model") rows(3) size(*0.8) symxsize(*0.8))   name(`stub', replace)
graph save graph/`stub'_median, replace
graph export graph/`stub'_median.pdf, replace
}

grc1leg playerconsumption networth
graph export graph/mediancomparison.pdf, replace

restore


*=============================
*Figure 3
*=============================

replace playerbalance_CA=playerbalance_TDA if sessiontype==1 | sessiontype==4 | sessiontype==11

preserve

collapse (median)  playerbalance_CA_median= playerbalance_CA    (mean) playerbalance_CA  , by(sessiontype subsessionround_in_sequence)
*merge SCF data
merge n:1 subsessionround_in_sequence using data/data
gen playerbalance_CA_median_rel=.
foreach i of numlist 1 4 5 6 11{
sum playerbalance_CA_median if subsessionround_in_sequence==16 & sessiontype==`i'
replace playerbalance_CA_median_rel=playerbalance_CA_median/r(mean)*100 if sessiontype==`i'
}

keep if subsessionround_in_sequence<=25 /*record the final questionnaire*/



twoway (line playerbalance_CA_median_rel subsessionround_in_sequence if sessiontype==1,   lwidth(2) lcolor(blue) ) (line playerbalance_CA_median_rel subsessionround_in_sequence if sessiontype==4 , lwidth(2) lcolor(blue) lp(dash_dot)) /*
*/(line playerbalance_CA_median_rel subsessionround_in_sequence if sessiontype==5,  lwidth(1) lcolor(red) lp(dash_dot)) (line playerbalance_CA_median_rel subsessionround_in_sequence if sessiontype==6 , lwidth(1) lcolor(red) lp(shortdash)) /*
*/(line playerbalance_CA_median_rel subsessionround_in_sequence if sessiontype==11 , lp(dot) lcolor(blue) lwidth(2))/*
*/ (line retqliq_p45 subsessionround_in_sequence if sessiontype==1, lp(longdash)  ) (line retqliq_p50 subsessionround_in_sequence if sessiontype==1, lp(longdash)  ) ///
(line retqliq_p55 subsessionround_in_sequence if sessiontype==1, lp(longdash)  ) (line retqliq_p60 subsessionround_in_sequence if sessiontype==1, lp(longdash)  ) ///
(line retqliq_p65 subsessionround_in_sequence if sessiontype==1, lp(longdash)  ) (line retqliq_p70 subsessionround_in_sequence if sessiontype==1, lp(longdash)  ) ///
, legend(order(1 "T1" 2 "T4" 3 "T5" 4 "T6" 5 "T1 model" 6 "p45" 7 "p50" 8 "p55" 9 "p60" 10 "p65" 11 "p70" ) rows(3)) xline(16)  ///
ytitle("Ages 60-61=100") xtitle("Period")
graph export "graph\scf.pdf", replace
restore
